<template>
  <div class="app-container">
    <div class="left-bar">
      <el-form :model="queryParams" ref="queryLeftForm" :inline="true" v-show="showSearch" label-width="68px" >
        <el-form-item label="工程编号" prop="projectId">
          <el-input
            v-model="queryParams.projectId"
            placeholder="请输入工程编号"
            clearable size="small"
            :style="{width: '100%'}"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
<!--        <el-form-item label="小区" prop="areaName">
            <el-select v-model="queryParams.areaName" :filter-method="filterMethod" placeholder="请输入小区" filterable allow-create
                       clearable size="small" @change="getGroup(queryParams.areaName)" default-first-option>
              <el-option v-for="(item, index) in areaIdOptions" :key="index" :label="item.areaName"
                         :value="item.areaName" :disabled="item.disabled" ></el-option>
            </el-select>
          </el-form-item>-->
          <el-form-item label="楼宇" prop="groupName">
            <el-select v-model="queryParams.groupName" placeholder="请输入楼宇" clearable size="small"  @change="getUnit(areaName1.areaName,queryParams.groupName)">
              <el-option v-for="(item, index) in groupIdOptions" :key="index" :label="item.groupName"
                         :value="item.groupName" :disabled="item.disabled" ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="单元" prop="unitName">
            <el-select v-model="queryParams.unitName" placeholder="请输入单元" clearable size="small" >
              <el-option v-for="(item, index) in unitNameOptions" :key="index" :label="item.unitName"
                         :value="item.unitName" :disabled="item.disabled" ></el-option>
            </el-select>
          </el-form-item>
        <el-form-item label="户" prop="doorName">
          <el-input
            v-model="queryParams.doorName"
            placeholder="请输入户"
            clearable size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" clearable size="small" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" clearable size="small" @click="resetLeftQuery">重置</el-button>
        </el-form-item>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              clearable size="small"
              icon="el-icon-edit"
              :disabled="single"
              @click="add"
            >添加</el-button>
          </el-col>
        </el-row>
      </el-form>

      <el-table v-loading="loading" :data="leftData"  @selection-change="saveLeft" ref="leftData" style="width: 100%" height="74%">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="地址" align="center" prop="addr" width="200"/>
        <el-table-column label="房屋编号" align="center" prop="houseId" />
        <el-table-column align="center" label="面积" prop="space" />
        <el-table-column :formatter="numberFormat"  align="center" label="预缴金额" prop="budgetMoney" />
        <el-table-column  :formatter="numberFormat"  align="center" label="账户余额" prop="balance" />
        <el-table-column  :formatter="numberFormat"  align="center" label="本息合计" prop="balanceSum" />
      </el-table>
    </div>

    <div class="right-bar">
      <el-form :model="queryParams" ref="queryRightForm" :inline="true" v-show="showSearch" label-width="68px" >
        <el-form-item label="房屋编号" prop="houseId">
          <el-input
            v-model="queryParams.houseId"
            placeholder="请输入工程编号"
            clearable size="small"
            :style="{width: '100%'}"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" clearable size="small" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" clearable size="small" @click="resetRightQuery">重置</el-button>
        </el-form-item>

        <div class="shan" stype="margin-top:500px;">
          <el-row :gutter="10" class="mb8" >
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                clearable size="small"
                icon="el-icon-delete"
                :disabled="multiple"
                @click="remove"
              >删除</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                clearable size="small"
                icon="el-icon-download"
                @click="handleCalculate"
              >试算</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                plain
                clearable size="small"
                type="success"
                icon="el-icon-download"
                @click="handleUpdate"
              >修改</el-button>
            </el-col>
          </el-row>
        </div>
      </el-form>

      <el-table :cell-style="tableRowClassName" v-loading="loading" :data="rightData" @selection-change="saveRight" ref="rightData" style="width: 100%" height="74%">
        <el-table-column type="selection" width="55" align="center" fixed/>
        <!--<el-table-column label="房屋编号" align="center" prop="houseId" fixed/>-->
        <el-table-column label="地址" align="center" prop="doorAddr" />
        <el-table-column label="面积" align="center" prop="space" />
        <el-table-column  :formatter="numberFormat"  align="center" label="分摊金额" prop="repairMoney" />
        <el-table-column  :formatter="numberFormat"  align="center" label="不足金额" prop="noMoney" />
        <el-table-column  :formatter="numberFormat"  align="center" label="拨付金额" prop="payMoney" />
      </el-table>
    </div>

    <!-- 添加或修改户资金修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工程编号" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="工程造价" prop="projectMoney">
          <el-input v-model="form.projectMoney" placeholder=""/>
        </el-form-item>
        <el-form-item label="试算单价" prop="price">
          <el-input v-model="form.price" placeholder=""/>
        </el-form-item>
        <el-form-item label="总面积" prop="space">
          <el-input v-model="form.space" placeholder=""/>
        </el-form-item>
        <el-form-item label="核算造价" prop="trueMoney">
          <el-input v-model="form.trueMoney" placeholder=""/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="trialCalculateForm">试算</el-button>
        <el-button type="primary" @click="checkCalculateForm">核算</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="titleDetail" :visible.sync="openDetail" append-to-body width="500px">
      <el-form ref="formDetail" :model="formDetail" :rules="rules" label-width="80px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input v-model="formDetail.houseId" placeholder="" :disabled="true"/>
        </el-form-item>
        <el-form-item label="分摊金额" prop="repairMoney">
          <el-input v-model="formDetail.repairMoney" placeholder=""/>
        </el-form-item>
        <el-form-item label="不足金额" prop="noMoney" >
          <el-input v-model="formDetail.noMoney" placeholder="" :disabled="true"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRepairDoor,
  getAreaInfo,
  getAreaInfo1,
  getGroupInfo,
  getUnitInfo,
  listRepairRight,
  getRepairDoor,
  delRepairDoor,
  insertRepairDoor,
  getProjectAreaName,
  updateRepairDoor
} from "@/api/repair/repairDoor";
import {getCalculate, updateCalculate} from "../../../api/repair/repairDoor";

export default {
  name: "RepairDoor",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单元维修明细表表格数据
      leftData: [],
      rightData: [],
      // 弹出层标题
      title: "",
      titleDetail: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        areaName: null,
        groupName: null,
        unitName: null,
        doorName: null,
      },
      areaIdOptions:[{}],
      groupIdOptions:[{}],
      unitNameOptions:[{}],
      areaName1: [],
      // 表单参数
      form: {},
      formDetail: {},
      formAreaName: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    numberFormat (row, column, cellValue) {
      cellValue += ''
      if (!cellValue.includes('.')) cellValue += '.'
      return cellValue.replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
        return $1 + ','
      }).replace(/\.$/, '')
    },
    filterMethod(val){
      this.value=val
      getAreaInfo1(this.value).then(response => {
        this.areaIdOptions = response.rows;
      });
    },
    areaSelect(){
      getAreaInfo().then(response => {
        this.areaIdOptions = response.rows;
      });
    },
    selectProjectAreaName() {
      if(this.queryParams.projectId==null){
        this.$modal.msgSuccess("请先输入工程编号");
      }else {
        getProjectAreaName(this.queryParams.projectId).then(response => {
          this.areaName1 = response.data;
          this.getGroup();
        });
      }
    },
    getGroup: function () {
      getProjectAreaName(this.queryParams.projectId).then(response => {
        this.areaName1 = response.data;
      });
      const areaName =  this.areaName1.areaName;
      if (areaName != "") {
        getGroupInfo(areaName).then(response => {
          this.groupIdOptions = response.rows;
        });
      }
    },
    getUnit(areaName,groupName){
      getUnitInfo(areaName,groupName).then(response => {
        this.unitNameOptions = response.rows;
      });
    },
    tableRowClassName({row}){
      return (row.balance<=row.budgetMoney*0.3?'color: red;':"")
    },

    getList() {
      debugger
      this.loading = true;
      listRepairDoor(this.queryParams).then(response => {
        this.leftData = response.rows;
        this.total = response.total;
        this.loading = false;
        this.selectProjectAreaName();
      });
      listRepairRight(this.queryParams).then(response => {
        this.rightData = response.rows;
      });
    },
/*    rightGetList() {
      debugger
      this.loading = true;
      listRepairRight(this.queryRightParams).then(response => {
        this.rightData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },*/
    // 取消按钮
    cancel() {
      this.open = false;
      this.openDetail = false;
      this.reset();
      this.resetDetail();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        doorId: null,
        areaName: null,
        groupName: null,
        unitName: null,
        doorName: null,
        houseId: null,
        space: null,
        budgetMoney: null,
        repairMoney: null,
        noMoney:null,
        afterBalance: null,
        payMoney: null,
        balance: null
      };
    },
    resetDetail() {
      this.formDetail = {
        projectId: null,
        houseId: null,
        repairMoney: null,
        noMoney: null
      };
      this.resetForm("formDetail");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetLeftQuery() {
      this.resetForm("queryLeftForm");
      this.handleQuery();
    },
    /** 重置按钮操作 */
    resetRightQuery() {
      this.resetForm("queryRightForm");
      this.rightList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.houseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    saveLeft(rows) {
      this.ids = rows.map(item => item.houseId)
      this.single = !rows.length>=1
      this.multiple = !rows.length
      this.left = [];
      if (rows) {
        rows.forEach(row => {
          if (row) {
            this.left.push(row);
          }
        });
      }
    },

    saveRight(rows) {
      this.ids = rows.map(item => item.houseId)
      this.projectId = rows.map(item => item.projectId)
      this.single = rows.length!==1
      this.multiple = !rows.length
      this.right = [];
      if (rows) {
        rows.forEach(row => {
          if (row) {
            this.right.push(row);
          }
        });
      }
    },
    // 右边表格选择项移到左边
    remove() {
      this.houseIds = []
      this.projectId=this.queryParams.projectId
      this.rightData.forEach(item=>{
        this.houseIds.push(item.houseId)
      })
      delRepairDoor(this.ids,this.projectId,this.rightData[0].houseId).then(response => {
        this.getList();
      });
    },

    rightList() {
      this.houseIds = []
      this.rightData.forEach(item=>{
        this.houseIds.push(item.houseId)
        this.projectId.push(item.projectId)
      })
      delRepairDoor(this.ids,this.projectId,this.rightData[0].houseId).then(response => {
        this.getList();
      });
    },

    add(){
      this.houseIds = []
      this.projectId=this.queryParams.projectId
      this.leftData.forEach(item=>{
        this.houseIds.push(item.houseId)
      })
      insertRepairDoor(this.ids,this.projectId,this.leftData[0].houseId).then(response => {
        this.getList();
      });
    },

    handleCalculate() {
      this.reset();
      getCalculate(this.queryParams).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "试算";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      debugger
      this.resetDetail();
      const houseId = row.houseId ||this.ids
      const projectId = row.projectId ||this.projectId
      getRepairDoor(houseId,projectId).then(response => {
        this.formDetail = response.data;
        this.openDetail = true;
        this.titleDetail = "修改";
      });
    },
    /** 试算 */
    trialCalculateForm() {
      getCalculate(this.form).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "试算";
      });
    },

    /** 核算 */
    checkCalculateForm() {
      debugger
      updateCalculate(this.form).then(response => {
        this.$modal.msgSuccess("核算成功");
        this.open = false;
        this.getList();
      });
    },
    /** 修改 */
    submitForm() {
      updateRepairDoor(this.formDetail).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.openDetail = false;
        this.getList();
      });
    },

  }
};
</script>

<style lang='postcss'>
.left-bar {

  padding: 0px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  height: 100%;


  float: left;
  left: 1%;
  width: 59%;
  height: 100%;
  position: absolute;
  overflow: auto;

}
.right-bar {

  padding: 0px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  height: 100%;

  float: right;
  left: 61%;
  width: 39%;
  height: 100%;
  position: absolute;
  overflow: auto;
}

.shan .mb8{
  margin-top:58px;
}

.header-main-left{
  position: fixed;
  top:108px;
  left: 240px;
  width: 55%;
}

.header-main-right{
  position: fixed;
  top:108px;
  right: 0px;
  width: 34%;
}
</style>
